package com.yanfei.zero.infrastructure.mq;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import com.yanfei.zero.infrastructure.config.DirectRabbitConfig;

import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class MqProducer {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final RabbitTemplate rabbitTemplate;
    
    private final RabbitAdmin rabbitAdmin;
    
    //发布消息
    public String sendMsgByTopicExchange(String msg, String routingKey) throws Exception {
        Map<String, Object> message = getMessage(msg);
        try {
            //发送消息
            rabbitTemplate.convertAndSend(DirectRabbitConfig.TOPIC_EXCHANGE_DEMO_NAME, routingKey, message);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
    
    
    //发布过期消息
    public String sendExpireMsg(String msg, String routingKey) throws Exception {
        Map<String, Object> message = getMessage(msg);
        try {
            //发送消息
            rabbitTemplate.convertAndSend(DirectRabbitConfig.TOPIC_EXCHANGE_DEMO_NAME, routingKey, message, new MessagePostProcessor() {  
                @Override  
                public Message postProcessMessage(Message message) throws AmqpException {  
                    // 设置消息的 TTL（单位：毫秒）  
                    message.getMessageProperties().setExpiration("3000");
                    return message;  
                }  
                
                
            });
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
    
    //发布延迟消息
    public String sendDelayMsg(String msg, String routingKey) throws Exception {
        Map<String, Object> message = getMessage(msg);
        try {
            //发送消息
            rabbitTemplate.convertAndSend(DirectRabbitConfig.TOPIC_EXCHANGE_DEMO_NAME, routingKey, message, new MessagePostProcessor() {  
                @Override  
                public Message postProcessMessage(Message message) throws AmqpException {  
                    // 设置延迟消息(订单30分钟未支付) 
                    message.getMessageProperties().setDelayLong(60000L);
                    return message;  
                }  
                
                
            });
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
    
    public void deleteAllQueues() {
    	    rabbitAdmin.deleteExchange(DirectRabbitConfig.TOPIC_EXCHANGE_DEAD);
    	    rabbitAdmin.deleteExchange(DirectRabbitConfig.TOPIC_EXCHANGE_DEMO_NAME);
    	    
            rabbitAdmin.deleteQueue(DirectRabbitConfig.TOPIC_EXCHANGE_QUEUE_A);
            rabbitAdmin.deleteQueue(DirectRabbitConfig.TOPIC_EXCHANGE_QUEUE_B);
            rabbitAdmin.deleteQueue(DirectRabbitConfig.TOPIC_EXCHANGE_QUEUE_C);
    }
    
    
    //组装消息体
    private Map<String, Object> getMessage(String msg) {
        String msgId = UUID.randomUUID().toString().replace("-", "").substring(0, 32);
        String sendTime = sdf.format(new Date());
        Map<String, Object> map = new HashMap<>();
        map.put("msgId", msgId);
        map.put("sendTime", sendTime);
        map.put("msg", msg);
        return map;
    }
    
}
